********************************************************************************
** 	TITLE: e14_demo_het.do
**
**	PROJECT: IGNITE
** 
**  PURPOSE: Appendix Figure XIV: Heterogeneity by Incarcerated Individual Characteristics

********************************************************************************
		
set sortseed 13
			
use "$output_data/estimate.dta", replace

cap xtile quartile_leadexp = pct18plusebll, n(4)
cap gen high_leadexp = quartile_leadexp==4
cap gen D_male = 1-D_female

***** interaction terms
est clear
mat R=J(100,7,.)
mat M=J(100,7,.)
foreach v in num_maj_weekly return_3m {
			//stay ignite_0 for misconduct
			if "`v'" == "num_maj_weekly"{
				cap drop stay
				gen stay = stay_misc
				cap drop ignite_0
				gen ignite_0 = ignite_0_misc
			}
			//stay ignite_0 for Recidivism
			if "`v'" == "return_3m"{
				cap drop stay
				gen stay = stay_rec
				cap drop ignite_0
				gen ignite_0 = ignite_0_rec
			}
	
	local t=1
	local groupvars `""high_leadexp" "prior_offense_1y" "age_17_24" "D_male" "D_Black""'    

	foreach gvar in `groupvars'{
		cap drop *_grpvar*

		foreach c of varlist ignite_0 stay D_ignite_proxy interact_proxy_any D_remove_any{
														
			gen `c'_grpvar = `c' * `gvar'
	
		}
			

***** IV Regression			
		ivreghdfe `v'  ( ignite_0_grpvar stay_grpvar ignite_0 stay = ///
						interact_proxy_any_grpvar D_remove_any_grpvar ///
						interact_proxy_any D_remove_any) ///
						D_ignite_proxy_grpvar D_ignite_proxy  `gvar' $auxiliary_control  ///
						if `v'>=0,  ///
						cluster(inmate) ///
						a($design_control )

	* IGNITE effect for bin 1				
		lincom ignite_0_grpvar+ignite_0
		
		local c=r(estimate)
		local ul=r(ub)
		local ll=r(lb)
		mat R[`t',1] = `c'
		mat R[`t',2] = `ul'
		mat R[`t',3] = `ll'
		mat R[`t',4] = `t'
	
	* IGNITE effect for bin 0 
		lincom ignite_0
		
		local c=r(estimate)
		local ul=r(ub)
		local ll=r(lb)
		mat R[`t',5] = `c'
		mat R[`t',6] = `ul'
		mat R[`t',7] = `ll'
	
	* Time-in-jail effect for bin 1				
		lincom stay_grpvar + stay
		
		local c=r(estimate)
		local ul=r(ub)
		local ll=r(lb)
		mat M[`t',1] = `c'
		mat M[`t',2] = `ul'
		mat M[`t',3] = `ll'
		mat M[`t',4] = `t'
	
	* Time-in-jail effect for bin 0 
		lincom stay
		
		local c=r(estimate)
		local ul=r(ub)
		local ll=r(lb)
		mat M[`t',5] = `c'
		mat M[`t',6] = `ul'
		mat M[`t',7] = `ll'
		local t = `t'+4						

	}
/*********************************************************************************
Panel (a) Effect of Months in IGNITE on Misconduct
Panel (b) Effect of Months in IGNITE on Recidivism
**********************************************************************************/		
***** Plot heterogeneity for IGNITE effects
		preserve	
			svmat R
			keep R1-R7
			drop if mi(R1)
			
			ren (R1 R2 R3 R4 R5 R6 R7) (b1 ul1 ll1 idvar b0 ul0 ll0)
			
			gen idvar0 = idvar-1
			
			replace b0 = b0/4 if idvar == 13
			replace ul0 = ul0/4 if idvar == 13
			replace ll0 = ll0/4 if idvar == 13
			
			local xlab -.4(.2).4
			if "`v'" == "return_3m"{
				local xlab -.3(.15).3
			}
			
			tw (rcap ul1 ll1 idvar, horizontal lc(black) mc(black)) ///
				(scatter idvar b1, mc(black) msize(medlarge)) ///
				(rcap ul0 ll0 idvar0, horizontal lc(black) mc(black)) ///
				(scatter idvar0 b0, mc(black) msize(medlarge)) ///
				, ///
				xline(0, lc(black)) ///
				yscale(range(-1 18)) ///
				ylab(0 "Low Lead Exposure" 1 "High Lead Exposure" ///
					4 "No Prior Offense" 5 "Prior Offense" ///
					8 "Age >24" 9 "Age 17-24" ///
					12 "Female" 13 "Male" ///
					16 "White" 17 "Black") ///
				xlab(`xlab') legend(off) ytitle("") xtitle("") ///
				aspect(1)
				
			gr export "$figures/`v'_demo_het_20240923.pdf", replace
		restore

/*********************************************************************************
Panel (c) Effect of Months in Jail on Misconduct
Panel (d) Effect of Months in Jail on Recidivism
**********************************************************************************/	
	* Plot heterogeneity for time-in-jail effects 
		preserve	
		svmat M
		keep M1-M7
		drop if mi(M1)
		
		ren (M1 M2 M3 M4 M5 M6 M7) (b1 ul1 ll1 idvar b0 ul0 ll0)
		
		gen idvar0 = idvar-1
		
		replace b0 = b0/4 if idvar == 13
		replace ul0 = ul0/4 if idvar == 13
		replace ll0 = ll0/4 if idvar == 13
		
		local xlab -.4(.2).4
		if "`v'" == "return_3m"{
			local xlab -.3(.15).3
		}
		
		tw (rcap ul1 ll1 idvar, horizontal lc(black) mc(black)) ///
			(scatter idvar b1, mc(black) msize(medlarge)) ///
			(rcap ul0 ll0 idvar0, horizontal lc(black) mc(black)) ///
			(scatter idvar0 b0, mc(black) msize(medlarge)) ///
			, ///
			xline(0, lc(black)) ///
			yscale(range(-1 18)) ///
			ylab(0 "Low Lead Exposure" 1 "High Lead Exposure" ///
				4 "No Prior Offense" 5 "Prior Offense" ///
				8 "Age >24" 9 "Age 17-24" ///
				12 "Female" 13 "Male" ///
				16 "White" 17 "Black" ) ///
			xlab(`xlab') legend(off) ytitle("") xtitle("") ///
			aspect(1)	

		gr export "$figures/`v'_demo_het_time_in_jail_20240923.pdf", replace
		restore
				}
				